require(['jquery','template','bootstrapPaginator','bootstrapValidator'], function ($,template) {
    $(function () {
        // 1.表格数据生成已经分页生成
        var page = 1, pageSize = 5
        function render() {
            $.ajax({
                url: '/category/queryTopCategoryPaging',
                type: 'get',
                data: {
                    page: page,
                    pageSize: pageSize
                },
                success: function (info) {
                    console.log(info)
                    $('tbody').html(template('tmp', info))
                    // 2.分页
                    $('.page_box ul').bootstrapPaginator({
                        bootstrapMajorVersion: 3,
                        currentPage: page,
                        totalPages: Math.ceil(info.total / pageSize),
                        size: "normal",
                        onPageClicked: function (a, b, c, p) {
                            page = p
                            render()
                        }
                    });
                }
            })
        }
        render()
    
        // 3.点击`添加分类`按钮
        $('.btn-addCategory').on('click', function () {
            $('#addCategory_model').modal('show')
        })
    
        // 4.表单检验
        $('form').bootstrapValidator({
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                categoryName: {
                    validators: {
                        notEmpty: {
                            message: '一级分类名称不能为空'
                        }
                    }
                }
            }
        })
    
        $('form').on('success.form.bv', function (e) {
            e.preventDefault();
            $.ajax({
                url: '/category/addTopCategory',
                type: 'post',
                data: $('form').serialize(),
                success: function (info) {
                    page = 1
                    render()
                    $('#addCategory_model').modal('hide')
                    $('form').data('bootstrapValidator').resetForm(true)
                }
            })
        })
    
    })
})